// miniprogram/pages/editAddress/editAddress.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    region: ['北京市', '北京市', '东城区'],
    province_id:1,
    city_id:2,
    region_id:3
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  bindRegionChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.getProvince(e.detail.value[0],1);
    this.getCity(e.detail.value[1],2);
    this.getRegion(e.detail.value[2],3);

    this.setData({
      region: e.detail.value
    })
  },

  getProvince:function(name,level){
    var that=this;
    wx.request({
      url: app.globalData.url +'/index.php?s=api/address/region_id',
      header: {
        'content-type': 'application/x-www-form-urlencoded',
      },
      method: 'POST',
      data: {name:name,level:level,app_id:10001},
      success: (res) => {
          if(res.data.code==1){
            that.setData({
              province_id:res.data.data.region_id
            })
          }
      }
    })
  },
  getCity:function(name,level){
    var that=this;
    wx.request({
      url: app.globalData.url +'/index.php?s=api/address/region_id',
      header: {
        'content-type': 'application/x-www-form-urlencoded',
      },
      method: 'POST',
      data: {name:name,level:level,app_id:10001},
      success: (res) => {
          if(res.data.code==1){
            that.setData({
              city_id:res.data.data.region_id
            })
          }
      }
    })
  },
  getRegion:function(name,level){
    var that=this;
    wx.request({
      url: app.globalData.url +'/index.php?s=api/address/region_id',
      header: {
        'content-type': 'application/x-www-form-urlencoded',
      },
      method: 'POST',
      data: {name:name,level:level,app_id:10001},
      success: (res) => {
          if(res.data.code==1){
            that.setData({
              region_id:res.data.data.region_id
            })
          }
      }
    })
  },
  formSubmit(e) {
    console.log('form发生了submit事件，携带数据为：', e.detail.value)
    var that=this;
    wx.request({
      url: app.globalData.url +'/index.php?s=api/address/add',
      header: {
        'content-type': 'application/x-www-form-urlencoded',
      },
      method: 'POST',
      data: {token:app.globalData.token,name:e.detail.value.name,phone:e.detail.value.phone,detail:e.detail.value.detail,province_id:that.data.province_id,city_id:that.data.city_id,region_id:that.data.region_id,app_id:10001},
      success: (res) => {
          if(res.data.code==1){
            wx.redirectTo({
              url: '../addressList/addressList'
            })
          }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})